package com.zhangwei.liwushuo.ui;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.RadioGroup;

import com.zbar.lib.CaptureActivity;
import com.zhangwei.liwushuo.BaseApp;
import com.zhangwei.liwushuo.R;
import com.zhangwei.liwushuo.bean.AppEntity;
import com.zhangwei.liwushuo.bean.Category;
import com.zhangwei.liwushuo.bean.Data;
import com.zhangwei.liwushuo.config.AppConstants;
import com.zhangwei.liwushuo.fragment.CategoryFragment_;
import com.zhangwei.liwushuo.fragment.DanPinFragment_;
import com.zhangwei.liwushuo.fragment.HomeFragment_;
import com.zhangwei.liwushuo.fragment.MeFragment_;
import com.zhangwei.liwushuo.utils.AsyncHttpHelper;
import com.zhangwei.liwushuo.view.TitleBarView;
import com.zw.library.utils.AppManager;
import com.zw.library.utils.FragmentTabUtils;
import com.zw.library.utils.SuperToastUtils;

import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;

@EActivity(R.layout.activity_main)
public class MainActivity extends BaseActivity implements TitleBarView.TitleBarClickListener, TitleBarView.TbCheckedChanged, FragmentTabUtils.OnRgsExtraCheckedChangedListener {
    @ViewById(R.id.main_rgs)
    RadioGroup mainRgs;
    @App
    BaseApp app;
    @ViewById
    TitleBarView commondTitlebar;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private final int home_index = 0;
    private final int danpin_index = 1;
    private final int category_index = 2;
    private final int me_index = 3;

    private long exitTime;

    @Bean
    AsyncHttpHelper httpHelper;


    public static final int QR_REQUEST_CODE = 0x2323;

    @Override
    public void afterInject() {
        fragments.add(new HomeFragment_());
        fragments.add(new DanPinFragment_());
        fragments.add(new CategoryFragment_());
        fragments.add(new MeFragment_());
    }

    @Override
    public void initView() {
        new FragmentTabUtils(getSupportFragmentManager(), fragments, R.id.fragment_container, mainRgs, this);
        commondTitlebar.setTbCheckedChanged(this);
        commondTitlebar.setTitleBarListener(this);
    }

    @Override
    public void initData() {
        AppEntity<Data<Category>> e = new AppEntity<Data<Category>>();
        e.getClass();

        httpHelper.get(AppConstants.URL_CATEGORIES_GIFT, e.getClass());
    }

    /**
     * 返回监听
     */
    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            SuperToastUtils.showShort(getString(R.string.app_exit_str));
            exitTime = System.currentTimeMillis();
        } else {
            AppManager.getAppManager().AppExit(app);
        }
    }

    @Override
    public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) {
        switch (index) {
            case home_index:
                commondTitlebar.showHomeTitle(getResources().getString(R.string.widget_footer_home_str));
                break;
            case danpin_index:
                commondTitlebar.showDanPinTitle(getResources().getString(R.string.widget_footer_sg_str));
                break;
            case category_index:
                commondTitlebar.showCategoryTitle();
                break;
            case me_index:
                commondTitlebar.showMeTitle(getResources().getString(R.string.widget_footer_me_str));
                break;
        }
    }

    @Override
    public void onTbCheckedChanged(RadioGroup group, int checkedId) {

    }

    @Override
    public void onTitleBarLinstener(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.titlebar_left:
                intent = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent, QR_REQUEST_CODE);
                break;
            case R.id.titlebar_right:
                intent = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent, QR_REQUEST_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == QR_REQUEST_CODE && data != null) {
//            String result = data.getStringExtra("result");
        }
    }
}
